package com.iotproject.eduservice.controller;


import com.iotproject.commonutils.R;
import com.iotproject.eduservice.client.VodClient;
import com.iotproject.eduservice.entity.EduSection;
import com.iotproject.eduservice.service.EduVideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-11-08
 */
@RestController
@RequestMapping("/eduservice/video")
//d @CrossOrigin
public class EduSectionController {

    @Autowired
    EduVideoService eduVideoService;

    @Autowired
    VodClient vodClient;


//    @DeleteMapping("/hello")
//    public R hello() {
//        return vodClient.hello("my id");
//    }



    //添加小节
    @PostMapping("addVideo")
    public R addVideo(@RequestBody EduSection eduSection) {
        eduVideoService.save(eduSection);
        return R.ok();
    }

    //根据小节id查询
    @GetMapping("getVideoInfo/{VideoId}")
    public R getVideoInfo(@PathVariable String VideoId) {
        EduSection eduSection = eduVideoService.getById(VideoId);
        return R.ok().data("video", eduSection);
    }

    //修改小节
    @PostMapping("updateVideo")
    public R updateVideo(@RequestBody EduSection eduSection) {
        eduVideoService.updateById(eduSection);
        return R.ok();
    }

//    @DeleteMapping("/hello/{id}")
//    public R hello(@PathVariable("id") String id) {
//        System.out.println("id is :" + id);
//        return vodClient.hello("my id");
//    }

    //删除
    @DeleteMapping("/{id}")
    public R deleteAlyiVideo(@PathVariable("id") String id) {
        //先通过videoId查找video记录，得到里面的videoSourceId
        EduSection eduSection = eduVideoService.getById(id);
        String videoSourceId = eduSection.getVideoSourceId();
        System.out.println("ID : " + videoSourceId);
        if (!StringUtils.isEmpty(videoSourceId)) {//如果有视频，先删除视频
            System.out.println("进");
            vodClient.deleteAlyiVideo(videoSourceId); //微服务调用
//            R res = vodClient.deleteAlyiVideo(videoSourceId);
//            if (res.getCode() == 20001) {
//                throw new GlobalException(20001, "熔断器执行");
//            }
        }
        //删除小节
        eduVideoService.removeById(id);
        return R.ok();
    }


}

